<template>
  <div class="flex" style="height: 100%">
    <div class="left mr12 bgFFF bdradius4">
      <div
        v-for="item in buttonLeft"
        :key="item.id"
        :class="criteria.chargeType == item.nameType ? 'active' : ''"
        @click="tabChange(item.nameType)">
        {{ item.meta.title }}
      </div>
    </div>
    <div class="right bgFFF bdradius4">
      <div class="card-box">
        <!--        <div class="title">缴费类型管理</div>-->
        <div class="mb16">
          <span class="color333 line16 mr10">缴费年份</span>
          <DatePicker
            v-model="pickDate"
            type="year"
            clearable
            placeholder="请选择缴费年份"
            format="yyyy"
            @on-change="handleDaterangeInput"
            :transfer="true"
            :editable="false"
            style="width: 180px"
          ></DatePicker>
        </div>

        <div class="card-item" v-for="item in listData" :key="item.id">
          <div class="flex space-between flex-y-center mb10">
            <div class="title" style="margin-bottom: 0;">缴费类型：{{ item.chargeType == 1 ? "社保" : "医保" }}</div>
            <!-- <Icon type="md-create" size="16" color="#1577FE" class="curPointer" @click="handleEdit"
                  v-if="buttonRight.find(item => item.name == 'bianji')"/> -->
            <img src="../../../assets/img/bj2.png" alt="" style="width:16px;height:16px;margin-right: 3px;cursor:pointer" @click="handleEdit" v-if="buttonRight.find(item => item.name == 'bianji')"/>
          </div>
          <div class="flex space-between flex-y-center">
            <div class="color333 line20">
              缴费金额：{{ item.chargeAmount }}元/年
            </div>
            <div class="color333 line20">缴费年份：{{ item.year }}年</div>
            <div class="color333 line20">
              缴费时间：{{item.pickDate2[0]}} - {{item.pickDate2[1]}}
              <!-- {{ parseTime(item.chargeStartDate, "{y}-{m}-{d}") }}～{{parseTime(item.chargeEndDate, "{y}-{m}-{d}")}} -->
              <!-- {{item.chargeStartDate.split(' ')[0] + '-' + item.chargeEndDate.split(' ')[0]}} -->
            </div>
            <!-- <div class="color333 line20">
              缴费方法：
              <span v-if="item.chargeWay == 0">暂无</span>
              <span v-if="item.chargeWay == 1">微信支付</span>
              <span v-if="item.chargeWay == 2">支付宝支付</span>
              <span v-if="item.chargeWay == 3">批量缴费支付</span>
            </div> -->
          </div>
        </div>
      </div>
    </div>
    <!--  编辑抽屉  -->
    <base-drawers
      v-model="showDrawers"
      title="编辑缴费"
      @input="cancelBt('showDrawers')"
      :loading="drawerLoading"
      submitBtn="提交"
      @drawerSubmit="drawerSubmit"
    >
      <div>
        <Form ref="form" :rules="rules" :label-width="105" :model="form">
          <FormItem prop="chargeType" label="缴费类型">
            <Select
              v-model="form.chargeType"
              transfer
              disabled
              style="width: 280px;"
              placeholder="请选择缴费类型"
            >
              <Option :value="2">医保</Option>
              <Option :value="1">社保</Option>
            </Select>
          </FormItem>
          <!-- <FormItem prop="pickDate" label="缴费年份">
            <DatePicker
              v-model="form.pickDate"
              type="year"
              clearable
              placeholder="请选择缴费年份"
              format="yyyy"
              @on-change="handleDaterangeInput1"
              :transfer="true"
              :editable="false"
              style="width: 280px"
            ></DatePicker>
          </FormItem> -->
          <FormItem prop="chargeAmount" label="缴费金额">
            <Input
              type="number"
              placeholder="请输入缴费金额"
              clearable
              v-model="form.chargeAmount"
              style="width: 280px"
          /></FormItem>
          <FormItem prop="pickDate2" label="缴费时间">
            <DatePicker
              v-model="form.pickDate2"
              type="daterange"
              clearable
              split-panels
              placeholder="请选择缴费时间"
              format="yyyy-MM-dd"
              @on-change="handleDaterangeInput2"
              :transfer="true"
              :editable="false"
              style="width: 280px"
            ></DatePicker>
          </FormItem>
          <!-- <FormItem prop="chargeWay" label="缴费方式">
            <Select v-model="form.chargeWay" transfer style="width: 280px" placeholder="请选择缴费方式">
              <Option :value="1">微信支付</Option>
              <Option :value="2">支付宝支付</Option>
              <Option :value="3">批量缴费支付</Option>
            </Select>
          </FormItem> -->
        </Form>
      </div>
    </base-drawers>
  </div>
</template>

<script>
import BaseDrawers from "_c/base-drawers/base-drawers";

export default {
  components: {
    BaseDrawers,
  },
  data() {
    return {
      buttonLeft: [],
      buttonRight: [],
      pickDate: "",
      criteria: {
        total: 0,
        page: {
          current: 1,
          size: 10,
        },
        chargeType: 2, // 医保
      },
      listData: [],

      // 编辑
      showDrawers: false,
      drawerLoading: false,
      form: {},
      rules: {
        chargeType: [
          {
            type: "number",
            required: true,
            message: "请选择缴费类型",
            trigger: "change",
          },
        ],
        // pickDate: [
        //   {
        //     type: "date",
        //     required: true,
        //     message: "请选择缴费年份",
        //     trigger: "change",
        //   },
        // ],
        chargeAmount: [
          {
            type: 'string',
            required: true,
            trigger: "change",
            validator:function (rule, value, callback) {
              var reg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/;
              if(!value) {
                callback("请输入缴费金额");
              } else if (!reg.test(value)) {
                callback("请输入正确的缴费金额");
              }else {
                callback();
              }
            }
          },
        ],
        pickDate2: [
          {
            type: "array",
            required: true,
            message: "请选择缴费时间",
            trigger: "change",
          },
        ],
        chargeWay: [
          {
            type: "number",
            required: true,
            message: "请选择缴费方式",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    let buttonMenu = this.buttonMenu(this.$route.name);
    buttonMenu.buttonLeft.map(item => {
      item.nameType = item.name === "shebao" ? 1 : 2;
      return item;
    })
    this.buttonLeft = buttonMenu.buttonLeft;
    if (this.buttonLeft.length) {
      this.buttonRight = this.buttonLeft[0].children;
      this.criteria.chargeType = this.buttonLeft[0].nameType;
    }

    //  缴费年份 默认选中明年
    let date = new Date().getFullYear() + 1;
    this.pickDate = new Date(Date.parse(date));
    this.criteria.year = date;
    this.getCharge();
  },
  methods: {
    tabChange(name) {
      let item = this.buttonLeft.find(item => item.name == name);
      if (item) {
        this.buttonRight = item.children;
      }
      this.criteria.chargeType = name;
      this.getCharge();
    },
    // 缴费年份
    handleDaterangeInput(data) {
      this.criteria.year = data;
      this.getCharge();
    },
    // 获取医保社保的缴费数据
    getCharge() {
      // let data = {
      //   ...this.criteria,
      // }
      // this.APIS.chargeList(data).then((res) => {
      //   if (res.code == 20000) {
      //     this.listData = res.data.records;
      //   } else {
      //     this.$Message.error(res.message)
      //   }
      // });
      let data = {
        chargeType: this.criteria.chargeType,
        year: this.criteria.year,
      };
      this.APIS.chargedetail(data).then((res) => {
        if (res.code == 20000) {
          if (res.data) {
            this.listData = [
              {
                chargeType: this.criteria.chargeType,
                year: this.criteria.year,
                chargeAmount: res.data.chargeAmount ? Number(res.data.chargeAmount)/100 : 0,
                pickDate2: [res.data.chargeStartDate.split(' ')[0],res.data.chargeEndDate.split(' ')[0]],
              },
            ];
          } else {
            this.listData = [
              {
                chargeType: this.criteria.chargeType,
                chargeAmount: 0,
                year: this.criteria.year,
                pickDate2: '',
                // chargeWay: 0,
                // name: "bianji",
              },
            ];
          }
        } else {
          this.$Message.error(res.message);
        }
      });
    },
    // 点击编辑按钮
    handleEdit() {
      this.form = {
        chargeType: this.listData[0].chargeType, //缴费类型1社保2医保
        pickDate: this.listData[0].year, //年份
        year: this.listData[0].year,//年份2
        chargeAmount: this.listData[0].chargeAmount == 0 ? '' : Number(this.listData[0].chargeAmount).toString(), //金额
        pickDate2: this.listData[0].pickDate2, //开始结束时间
        startDate: this.listData[0].pickDate2 == '' ? "" : this.listData[0].pickDate2[0],
        // endDate: this.listData.pickDate2[1],
        // chargeWay: this.listData[0].chargeWay, //缴费方式
      };
      this.drawerLoading = false;
      this.showDrawers = true;
    },
    // 缴费年份
    handleDaterangeInput1(data) {
      this.form.year = data;
    },
    // 缴费时间
    handleDaterangeInput2(data) {
      this.form.chargeStartDate = data[0];
      this.form.chargeEndDate = data[1];
    },
    // 修改或者新增缴费信息
    drawerSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.drawerLoading = true
          var data = {
            chargeType: this.form.chargeType,
            year: this.form.year,
            chargeAmount: this.form.chargeAmount,
            startDate: this.form.chargeStartDate,
            endDate: this.form.chargeEndDate,
            remark: "",
          };
          this.APIS.saveorupdatecharge(data).then((res) => {
            this.drawerLoading = false;
            if (res.code == 20000) {
              this.$Message.success("编辑成功");
              this.cancelBt("showDrawers");
              this.getCharge();
            } else {
              this.$Message.error(res.message);
            }
          });
        }
      });
    },
    // 关闭抽屉
    cancelBt(drawerName) {
      this[drawerName] = false;
      this.form = {};
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style scoped lang="less">
/deep/.ivu-select-selected-value{
  font-size: 14px !important;
}
.left {
  height: 100%;
  padding: 20px;

  div {
    color: #333333;
    line-height: 20px;
    margin-bottom: 20px;
    cursor: pointer;
  }

  .active {
    color: #1577fe;
    font-weight: bold;
  }
}

.right {
  flex: 1;
}

.card-box {
  padding: 16px 20px;
  background-color: #ffffff;
  border-radius: 4px;
  margin-bottom: 12px;
}

.title {
  font-weight: 600;
  color: #333333;
  line-height: 20px;
  margin-bottom: 16px;
}

.card-item {
  box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.08);
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}

.card-item:last-child {
  margin-bottom: 0;
}

/deep/ .ivu-modal-body {
  padding: 30px 73px;
}
</style>
